package com.heima.search.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.vos.SearchArticleVo;
import com.heima.search.service.SearchService;
import com.heima.utils.thread.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-11-19 10:24
 */
@RestController
@RequestMapping("/api/v1/article/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @PostMapping("/search")
    public ResponseResult search(@Valid @RequestBody UserSearchDto dto){

        List<SearchArticleVo> vos =  searchService.search(dto);

//        if(dto.getIsSearch() != null && dto.getIsSearch() == 1){
            //修改为异步注解之后，将用户id手动传递到方法中
            Integer userId = AppThreadLocalUtil.getUser().getId();
            searchService.saveSearchHistory(dto.getSearchWords(),userId);
//        }

        return ResponseResult.okResult(vos);
    }
}
